!function(window,document,$,undefined) {
    
   var app = angular.module('myApp',[]);

   app.controller('ctrl',function($scope,$http) {
    

    var arr = [];
     $scope.pageSize = 5;
     $scope.isActive = false;
     $scope.page = 0;

       
    // 映射
    
    $scope.status = {
        0 : '上架',
        1 : '下架'
    };

    $scope.borrowStatus = {
        0 : '未借出',
        1 : '已借出'
    };


    $scope.classify = [
         {"id":1,name:'历史类'},
         {"id":2,name:'军事类'},
         {"id":3,name:'科学类'},
         {"id":4,name:'小说类'}
    ];


      //初始化渲染列表
     renderList();
     // 初始化调用时间插件
      upDateTime();

    
     // 定义一个ng-show的值
     
     $scope.isShow = false;


     //渲染列表
     
    function renderList() {
        $http({
        url:'api/books_list.php',
        params:{query:$scope.query,size:$scope.pageSize,page:$scope.page},
        method:'get'
             
         }).success(function(response) {
            $scope.books = response.data;
            $scope.pages = Math.ceil(response.total / $scope.pageSize);
            $scope.pageArr = [];
            for(var i=1; i <= $scope.pages; i++) {
                $scope.pageArr.push(i);
            }
           
         });
    };

    // 分页的点击事件
     $scope.pageClick = function() {
         $scope.page = this.$index;
         $scope.currentIndex = this.$index;
         console.log($scope.page )
         renderList();
     }

    //最后一页 第一页事件
    
    $scope.firstClick = function() {
        $scope.page = 0; 
       renderList();
    }

    $scope.lastClick = function(){
       $scope.page = $scope.pageArr.length - 1;
       
      renderList();
    }


     //新增图书
     $scope.addBook = function() {
          $scope.bookInfo = '';
          $scope.isShow = false;
          $('#dialog').modal();
          
     };

     // 执行新增事件
      
    $scope.actionAdd = function() {
        $http({
           url:"api/books_add.php",
           method:'get',
           params: $scope.bookInfo
        }).success(function(response) {
              $('#dialog').modal('hide');
              renderList();
        });
    };

     //修改图书
     $scope.updateBook = function() {
          if(!($('tbody input:checked').length == 1)) {
             alert('别乱点！');
             return
          }else {
              angular.forEach($scope.books,function(i) {
                   if(i.checkOne) {
                     $scope.bookInfo = i;
                   }
              })
             $scope.isShow = true;
             $('#dialog').modal();
          }
     };

     //执行修改图书

     $scope.actionUpdate = function() {
        
          $http({
           url:"api/books_update.php",
           method:'get',
           params: $scope.bookInfo
        }).success(function(response) {
              $('#dialog').modal('hide');
              renderList();
        });
     }

       // 删除图书
     $scope.delBook = function() {
          
          if(!($('tbody input:checked').length > 0)) {
                alert('别乱点！');
            }else {
               if(confirm('想好了是吧')) {
                  for(var i = 0; i<arr.length; i++) {
                     $http({
                         url:'api/books_del.php',
                         params:{ids:arr[i]}
                      }).success(function(response) {
                            renderList();
                      })
                  };
                     alert('删除成功！');
                     arr = [];
                };
            };
     };

     $scope.searchBook = function() {
         renderList();
     }

     $scope.changeInfo = function() {
        renderList();
     }

     /////////
     //日期组件 //
     /////////
            function upDateTime() {
          $('#p_date').datetimepicker({
              language:  'zh-CN',
              weekStart: 1,
              todayBtn:  1,
              autoclose: 1,
              todayHighlight: 1,
              startView: 2,
              minView: 2,
              forceParse: 0
              });
       };

       //复选框是否事件
           
     $scope.allClick = function() {
              arr = [];
              angular.forEach($scope.books,function(i) {
                  if($scope.checkAll) {
                       i.checkOne = true
                       arr.push(i.id);
                  }else {
                       i.checkOne = false
                  }
              })
           };
              
      $scope.oneClick = function() {
              var index = 0;
               
               if(this.book.checkOne) {
                   arr.push(this.book.id);
               }else {
                   arr.splice(arr.indexOf(this.book.id),1)
               }
              
              if(arr.length == $scope.books.length) {
                       $scope.checkAll = true;
                    }else {
                        $scope.checkAll = false;
              };
      } ; 
   });

   


}(window,document,jQuery);

